/*
 * Copyright (c) 2008, Spagettikod | Stockholm
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the name of the Spagettikod | Stockholm nor the names of its
 *     contributors may be used to endorse or promote products derived from
 *     this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package com.spagettikod.t437;

import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

public class AccessKeyDialog extends JDialog implements ActionListener, CaretListener, ItemListener
{

	private static final long serialVersionUID = -8468841142185398086L;

	private static final JTextField accessKeyField = new JTextField(20);

	private static final JTextField secretKeyField = new JTextField(20);

	private static final JLabel accessKeyLabel = new JLabel("Access Key:");

	private static final JLabel secretKeyLabel = new JLabel("Secret Access Key:");

	private static final JButton okButton = new JButton("OK");

	private static final JButton cancelButton = new JButton("Cancel");

	private static final JCheckBox saveSecretCheckBox = new JCheckBox("Remember secret access key");

	private Configurator config;

	public AccessKeyDialog(Frame parent, Configurator config)
	{
		super(parent, true);

		this.setTitle("AWS Access Key Setup");

		this.config = config;

		JPanel pane = new JPanel(new GridBagLayout());

		GridBagConstraints c = new GridBagConstraints();
		c.anchor = GridBagConstraints.LINE_START;
		c.insets = new Insets(4, 4, 4, 4);
		c.gridx = 0;
		c.gridy = 0;
		pane.add(accessKeyLabel, c);

		c.gridx = 1;
		pane.add(accessKeyField, c);
		accessKeyField.setText(config.getAccessKey());
		accessKeyField.setActionCommand("accessKeyField");
		accessKeyField.addCaretListener(this);

		// Secret access key
		c.gridx = 0;
		c.gridy = 1;
		pane.add(secretKeyLabel, c);

		c.gridx = 1;
		c.insets = new Insets(4, 4, 0, 4);
		pane.add(secretKeyField, c);
		secretKeyField.setText(config.getSecretKey());
		secretKeyField.setActionCommand("secretKeyField");
		secretKeyField.addCaretListener(this);

		// Save secret key checkbox
		c.gridx = 1;
		c.gridy = 2;
		c.gridwidth = 2;
		c.anchor = GridBagConstraints.LINE_START;
		c.insets = new Insets(0, 10, 0, 0);
		pane.add(saveSecretCheckBox, c);
		saveSecretCheckBox.addItemListener(this);

		// Buttons
		JPanel buttonPane = new JPanel(new FlowLayout());

		cancelButton.setActionCommand("cancel");
		cancelButton.addActionListener(this);
		cancelButton.setMnemonic(KeyEvent.VK_ESCAPE);

		okButton.setActionCommand("ok");
		okButton.addActionListener(this);
		validateOKButton();

		buttonPane.add(okButton);
		buttonPane.add(cancelButton);

		c.gridy = 3;
		c.anchor = GridBagConstraints.LINE_END;
		c.insets = new Insets(2, 2, 2, 2);
		pane.add(buttonPane, c);

		getRootPane().setDefaultButton(cancelButton);

		getContentPane().add(pane);

		pack();
		setVisible(true);
	}

	private void validateOKButton()
	{
		if (accessKeyField.getText().equals("") || secretKeyField.getText().equals(""))
		{
			okButton.setEnabled(false);
		}
		else
		{
			okButton.setEnabled(true);
		}
	}

	public void actionPerformed(ActionEvent e)
	{
		if ("cancel".equals(e.getActionCommand()))
		{
			this.dispose();
		}
		else if ("ok".equals(e.getActionCommand()))
		{
			config.setAccessKey(accessKeyField.getText());
			config.setSecretKey(secretKeyField.getText());
			config.save();
			this.dispose();
		}
	}

	public void caretUpdate(CaretEvent e)
	{
		validateOKButton();
	}

	public void itemStateChanged(ItemEvent e)
	{
		if (e.getStateChange() == ItemEvent.DESELECTED)
		{
			config.setSaveSecretKey(false);
		}
		else
		{
			config.setSaveSecretKey(true);
		}
	}
}
